package engine.systems.ui.dial
{
	import ash.core.Component;
	
	public class Dial extends Component
	{
		public var invalidate:Boolean = false;
		
		public var rate:Number = 5;
		public var ease:Boolean = true;
		public var forward:Boolean = true;
		public var horizontal:Boolean;
		public var offset:Number;
		
		private var _index:int;
		private var _values:Array;
		private var _numVisible:int;
		
		public var oldIndex:int;
		public var distance:Number = 0;
		
		//NumVisible should always be an odd number. Dials should have one "selected" item in their "center".
		public var prefix:String;
		
		public function Dial(values:Array, index:int = 1, prefix:String = "dial", numVisible:int = 1, offset:Number = 50, horizontal:Boolean = true)
		{
			_values = values;
			_index = index;
			this.oldIndex = _index;
			_numVisible = numVisible;
			
			this.prefix		= prefix;
			this.offset 	= offset;
			this.horizontal = horizontal;
		}
		
		override public function destroy():void
		{
			_values = null;
		}
		
		public function get index():int { return _index; }
		public function set index(index:int):void
		{
			if(index < 0) 						{ while(index < 0) index += _values.length; }
			else if(index >= _values.length)	{ while(index >= _values.length) index -= _values.length; }
			
			if(index == _index) return;
			
			this.oldIndex = _index;
			if(forward)
			{
				while(_index != index)
				{
					distance += offset;
					if(++_index >= _values.length) _index = 0;
				}
			}
			else
			{
				while(_index != index)
				{
					distance += offset;
					if(--_index < 0) _index = values.length - 1;
				}
			}
			
			invalidate = true;
		}
		
		public function get values():Array { return _values; }
		public function set values(values:Array):void
		{
			if(!values) return;
			
			_values = values;
			
			this.invalidate = true;
		}
		
		public function get numVisible():int { return _numVisible; }
		public function set numVisible(numVisible:int):void
		{
			
		}
	}
}